import { Component, OnInit } from '@angular/core';
import { InteractiveService } from 'src/app/index/interactive.service';
import * as echarts from 'echarts';

declare var security: any;
declare var form: any;
declare var ui: any;
declare var base: any;

@Component({
  selector: 'app-protal2',
  templateUrl: './protal2.component.html',
  styleUrls: ['./protal2.component.scss']
})
export class Protal2Component implements OnInit {


  ss: any = null;
  sbs: any = null;

  user: any = {};
  // 总数据
  rowCounts = 0;
  // 统一待办数据
  fs: any = null;
  // 流程统计数据
  statistics: { [index: string]: any } = {};
  // 待办菜单控制器
  menu = null;

  // 安保人员
  abs: any = null;
  absdimensions: any;
  abssource: any[];

  // 通知公告
  noticeFs: any = null;


  constructor(public is: InteractiveService) {
    this.ss = security.SecurityService;
    this.user = this.ss.user;
    this.menu = new ui.TabService('all', 'active');

    this.fs = new form.FormSimpleService('portalworklist', {
      orderObject: { id: 0 },
      keyField: 'id',
      randomFields: ['id']
    });
    this.fs.statisticsCallback = (data) => {
      this.statistics = {};
      this.statistics.htgl = 0;
      this.statistics.bxgl = 0;
      this.statistics.rsgl = 0;
      this.statistics.lcgl = 0;
      this.statistics.gsgl = 0;

      data.forEach(s => {
        if (s.name === '合同管理') {
          this.statistics.htgl += s.count;
        } else if (s.name === '报销管理') {
          this.statistics.bxgl += s.count;
        } else if (s.name === '人事管理') {
          this.statistics.rsgl += s.count;
        } else if (s.name === '流程管理') {
          this.statistics.lcgl += s.count;
        } else if (s.name === '工时管理') {
          this.statistics.gsgl += s.count;
        }
      });
    };
    this.reflash();

    this.abs = new form.FormSimpleService('securityperson', {
      orderObject: { id: 0 },
      keyField: 'id',
      randomFields: ['id'],
      pageSize: 99999
    });

    this.sbs = new form.FormSimpleService('securityequipment', {
      orderObject: { id: 0 },
      keyField: 'id',
      randomFields: ['id'],
      pageSize: 99999
    });
    this.sbs.loadData(true);

    this.noticeFs = new form.FormSimpleService('portalnotice', {
      orderObject: { fqsj: 1 },
      keyField: 'id',
      randomFields: ['id']
    });
    this.noticeFs.loadData(true);
  }

  ngOnInit() {
    this.is.regRouter();
    this.initAbryCharts();
    this.initAbsbCharts();
  }

  bpmNote(item) {
    if (item) {
      window.open('#protal_noticeinfo_mis?info=true&id=' + item.id);
    } else {
      window.open('#protal_noticeinfo_mis?info=true');
    }
  }

  // 打开流程表单
  bpmEdit(item) {
    if (item) {
      window.open(item.prjurl + '#' + item.prjvi + '?info=true&id=' + item.sbussinessid);
    } else {
      window.open(item.prjurl + '#' + item.prjvi + '?info=true');
    }
  }

  // 刷新
  reflash() {
    this.menu.setTab('all');
    this.fs.resetQueryObject();
    this.fs.defaultQueryObject = {};
    this.fs.loadData(true, () => {
      this.rowCounts = this.fs.rowsCount;
    });
    this.fs.statistics();
  }

  // 查询过滤
  bpmSearch(item) {
    this.menu.setTab(item);
    let g = '';
    if (item === 'htgl') {
      g = '合同管理';
    } else if (item === 'bxgl') {
      g = '报销管理';
    } else if (item === 'rsgl') {
      g = '人事管理';
    } else if (item === 'lcgl') {
      g = '流程管理';
    } else if (item === 'gsgl') {
      g = '工时管理';
    }
    this.fs.resetQueryObject();
    this.fs.defaultQueryObject = { group: g };
    this.fs.loadData(true);
  }

  // 统计图
  initAbryCharts() {
    const absdata = [];
    const url = base.BaseService.getHostUrl() + '/securityperson/getAllData';
    base.BaseService.post(url, '1', (datas) => {
      for (const key in datas) {
        datas[key].product = key;
        absdata.push(datas[key]);
      }
      const ec = echarts as any;
      const lineChart = ec.init(document.getElementById('box_abry'));
      const lineChartOption = {
      legend: {},
      tooltip: {},
      dataset: {
        dimensions: ['product', '无证', '二级安检', '三级安检', '四级安检', '五级安检'],
        source: absdata
      },
      xAxis: {type: 'category'},
        axisLabel: {
        interval:   0,
          rotate: 40
        },
        yAxis: {},
      // Declare several bar series, each will be mapped
      // to a column of dataset.source by default.
      series: [
        {type: 'bar'},
        {type: 'bar'},
        {type: 'bar'},
        {type: 'bar'},
        {type: 'bar'}
      ]
    };
      lineChart.on('updateAxisPointer', function(event) {
      const xAxisInfo = event.axesInfo[0];
      if (xAxisInfo) {

        const dimension = xAxisInfo.value + 1;
        lineChart.setOption({
          series: {
            id: 'pie',
            label: {
              formatter: '{b}: {@[' + dimension + ']} ({d}%)'
            },
            encode: {
              value: dimension,
              tooltip: dimension
            }
          }
        });
      }
    });
      lineChart.setOption(lineChartOption);
    });
  }

  initAbsbCharts() {
    const legenddata = [];
    const sbsdata = [];
    const url = base.BaseService.getHostUrl() + '/securityequipment/getAllData';
    base.BaseService.post(url, '1', (datas) => {
      for (const key in datas) {
        const Nv = {name: '', value: 0};
        Nv.name = key;
        Nv.value = datas[key];
        sbsdata.push(Nv);
        legenddata.push(key);
      }

    const ec = echarts as any;
    const lineChart = ec.init(document.getElementById('box_absb'));
    const lineChartOption = {
      title: {
        text: '安保设施',
        // subtext: '虚构数据',
        left: 'center'
      },
      tooltip: {
        trigger: 'item',
        formatter: '{a} <br/>{b} : {c} ({d}%)'
      },
      legend: {
        // orient: 'vertical',
        // top: 'middle',
        bottom: 10,
        left: 'center',
        data: legenddata
      },
      series: [
        {
          name: '设备类型',
          type: 'pie',
          radius: '65%',
          center: ['50%', '50%'],
          selectedMode: 'single',
          data: sbsdata,
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    };

      lineChart.setOption(lineChartOption);
    });
  }
  openWin(path) {
    window.open('#' + path + '?info=true');
  }
}
